// pages/home/home.js
const { formatObjToUrl } = require('../../utils/util')
const app = getApp()

Page({
  
  data: {
    background: [
      'https://skl-static.oss-cn-hangzhou.aliyuncs.com/graduate_weixin_app/swiper_1.jpg', 
      'https://skl-static.oss-cn-hangzhou.aliyuncs.com/graduate_weixin_app/swiper_2.jpg',
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 500,
    examLevel: -1,
    examType: -1,
    practiceLevel: -1
  },

  // 选择章节练习等级
  handleChoosePracticeLevel() {
    let _this = this;
    wx.showActionSheet({
      alertText: '请选择题目等级',
      itemList: ['一级', '二级'],
      success (res) {
        _this.setData({
          practiceLevel: res.tapIndex + 1
        })
        _this.handleSuits()
      },
      fail (res) {
        _this.setData({
          practiceLevel: -1,
        })
      }
    })
  },

  // 章节练习
  handleSuits() {
    let _this = this;
    wx.showActionSheet({
      alertText: '请选择题目类型',
      itemList: ['选择题', '操作题'],
      success (res) {
        if(res.tapIndex === 0) {
          // 选择题
          _this.goToSuits(2)
        } else if(res.tapIndex === 1) {
          // 操作题
          _this.goToSuits(1)
        }
      },
      fail (res) {
        console.log('您已取消选择')
      }
    })
  },

  // 操作题
  handleOperate() {
    this.goToSuits(1)
  },

  // 选择题
  handleChoice() {
    this.goToSuits(2)
  },

  // 错题
  handleFault() {
    app.axios({
      url: `/answer/my_error`
    }).then(res => {
      if(res.code === 0) {
        // 没有错题不跳转 给提示
        if(res.result.length === 0) {
          wx.showToast({
            title: '暂无错题',
            icon: 'none'
          })
          return;
        } else {
          // 有错题跳转
          wx.navigateTo({
            url: '../fault/fault',
          })
        }
      }
    })
  },

  // 跳转页面选择哪一套试卷
  goToSuits(suitType) {
    const options = formatObjToUrl({
      suitType,
      level: this.data.practiceLevel
    })
    wx.navigateTo({
      url: `../suits/suits${options}`,
    })
  },

  // 选择模拟考试等级
  handleChooseExamLevel() {
    let _this = this;
    wx.showActionSheet({
      alertText: '请选择考试等级',
      itemList: ['一级', '二级'],
      success (res) {
        _this.setData({
          examLevel: res.tapIndex + 1
        })
        _this.handleChooseExamType()
      },
      fail (res) {
        _this.setData({
          examLevel: -1,
          examType: -1
        })
      }
    })
  },

  // 选择模拟考试题型
  handleChooseExamType() {
    let _this = this;
    wx.showActionSheet({
      alertText: '请选择考试题型',
      itemList: ['操作题', '选择题'],
      success (res) {
        _this.setData({
          examType: res.tapIndex + 1
        })
        _this.goToExam()
      },
      fail (res) {
        _this.setData({
          examLevel: -1,
          examType: -1
        })
      }
    })
  },

  // 跳转到考试页面
  goToExam() {
    const options = {
      level: this.data.examLevel,
      type: this.data.examType
    }
    wx.navigateTo({
      url: `../exam/exam${formatObjToUrl(options)}`,
    })
  },

  // 考试记录
  handleRecords() {
    app.axios({
      url: `/score/select_score`
    }).then(res => {
      if(res.code === 0) {
        // 没有考试记录 不跳转 给提示
        if(res.result.length === 0) {
          wx.showToast({
            title: '暂无考试记录',
            icon: 'none'
          })
          return;
        } else {
          // 有考试记录 跳转
          wx.navigateTo({
            url: `../records/records`,
          })
        }
      }
    })
  },
  handleDetail() {
    let _this = this;
    wx.showActionSheet({
      itemList: ['一级级MS Office应用','二级公共基础知识大纲','二级MS Office高级应用'],
      success (res) {
        _this.setData({
          practiceLevel: res.tapIndex + 1
        })
        if(res.tapIndex === 0) {
          wx.downloadFile({
            url: 'http://80-chong-front.oss-cn-hangzhou.aliyuncs.com/images/%E4%B8%80%E7%BA%A7%E7%BA%A7MS%20Office%E5%BA%94%E7%94%A8.pdf',
            success: function (res) {                           
             console.log(res);
             if (res.statusCode === 200) {                     //成功
               var Path = res.tempFilePath                     //返回的文件临时地址，用于后面打开本地预览所用
               wx.openDocument({
                 filePath: Path,                               //要打开的文件路径
                 success: function (res) {
                   console.log('打开PDF成功');
                 }
               })
             }
           },
          })
        } else if(res.tapIndex === 1) {
          wx.downloadFile({
            url: 'http://80-chong-front.oss-cn-hangzhou.aliyuncs.com/images/%E4%BA%8C%E7%BA%A7%E5%85%AC%E5%85%B1%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E5%A4%A7%E7%BA%B2.pdf',
            success: function (res) {                           
             console.log(res);
             if (res.statusCode === 200) {                     //成功
               var Path = res.tempFilePath                     //返回的文件临时地址，用于后面打开本地预览所用
               wx.openDocument({
                 filePath: Path,                               //要打开的文件路径
                 success: function (res) {
                   console.log('打开PDF成功');
                 }
               })
             }
           },
          })
        } else if(res.tapIndex === 2){
          wx.downloadFile({
            url: 'http://80-chong-front.oss-cn-hangzhou.aliyuncs.com/images/%E4%BA%8C%E7%BA%A7MS%20Office%E9%AB%98%E7%BA%A7%E5%BA%94%E7%94%A8.pdf',
            success: function (res) {                           
             console.log(res);
             if (res.statusCode === 200) {                     //成功
               var Path = res.tempFilePath                     //返回的文件临时地址，用于后面打开本地预览所用
               wx.openDocument({
                 filePath: Path,                               //要打开的文件路径
                 success: function (res) {
                   console.log('打开PDF成功');
                 }
               })
             }
           },
          })
        }
       
      },
      fail (res) {
        _this.setData({
          practiceLevel: -1,
        })
      }
    })
  },
  goToSuit2(suitType) {
    const options = formatObjToUrl({
      suitType,
      level: 3
    })
    wx.navigateTo({
      url: `../suits/suits${options}`,
    })
  },
  handleSpecial(){
    let _this = this;
    _this.goToSuit2(2)
  }
})